<template>
  <uni-popup ref="popup" type="dialog">
    <uni-popup-dialog
      mode="base"
      message="成功消息"
      title="确认下单吗"
      type="warn"
      :content="`寄件费用为${price}元`"
      :before-close="true"
      @close="close"
      @confirm="confirm"
    ></uni-popup-dialog>
  </uni-popup>
</template>

<script>
import storageOperation from "/utils/index";
import mailAPI from "/api/modules/mail";
export default {
  name: "warn-popup",
  props: {
    price: {
      type: Number,
      default: 0,
    },
  },
  methods: {
    open() {
      this.$refs.popup.open();
    },
    close() {
      this.$refs.popup.close();
    },
    confirm() {
      this.close();
      this.doRequest();
    },
    doRequest() {
      const userName = storageOperation.getStorageSync("userName").value;
      const {
        subUserName: mailUserName,
        subUserPhone: mailUserPhone,
        subUserAddress: mailAddress,
      } = this.$store.getters.getSelectObj_mail;
      const {
        subUserName: pickUpUserName,
        subUserPhone: pickUpUserPhone,
        subUserAddress: pickUpAddress,
      } = this.$store.getters.getSelectObj_pickUp;
      const {
        subUserName: enterpriseName,
        subUserPhone: enterprisePhone,
        subUserAddress: enterpriseAddress,
      } = this.$store.getters.getSelectObj_goodsReject;
      const {
        reason: reason0,
        mode: mode0,
        visitTime: visitTime0,
        stageName: stageName0,
        goodsInformation: goodsInformation0,
      } = this.$store.getters.getMailOtherInfo;
      const {
        reason: reason1,
        mode: mode1,
        visitTime: visitTime1,
        stageName: stageName1,
        goodsInformation: goodsInformation1,
      } = this.$store.getters.getGoodsRejectedOtherInfo;
      const params1 = {
        reason: reason0,
        userName,
        mailUserName,
        mailUserPhone,
        mailAddress,
        pickUpUserName,
        pickUpUserPhone,
        pickUpAddress,
        mode: mode0,
        visitTime: visitTime0,
        goodsInformation: goodsInformation0,
      };
      const params2 = {
        reason: reason0,
        userName,
        mailUserName,
        mailUserPhone,
        mailAddress,
        pickUpUserName,
        pickUpUserPhone,
        pickUpAddress,
        mode: mode0,
        stageName: stageName0,
        goodsInformation: goodsInformation0,
      };
      const params3 = {
        reason: reason1,
        userName,
        mailUserName,
        mailUserPhone,
        mailAddress,
        enterpriseName,
        enterprisePhone,
        enterpriseAddress,
        mode: mode1,
        visitTime: visitTime1,
        goodsInformation: goodsInformation1,
      };
      const params4 = {
        reason: reason1,
        userName,
        mailUserName,
        mailUserPhone,
        mailAddress,
        enterpriseName,
        enterprisePhone,
        enterpriseAddress,
        mode: mode1,
        stageName: stageName1,
        goodsInformation: goodsInformation1,
      };
      if (reason0 === 0) {
        if (mode0 === 0) {
          //上门取件
          mailAPI
            .insertMailReason0WithVisitTime(params1)
            .then((res) => {
              console.log(res);
              uni.showToast({
                title: "下单成功",
                icon: "success",
                position: "top",
                duration: 1000,
              });
              setTimeout(() => {
                uni.navigateTo({
                  url: "/pages/mail/sent-off-check",
                });
              }, 2000);
            })
            .catch((err) => {
              uni.showToast({
                title: "下单失败",
                icon: "error",
                position: "top",
                duration: 1000,
              });
              console.log(err);
            });
        } else {
          mailAPI
            .insertMailReason0WithStageName(params2)
            .then((res) => {
              console.log(res);
              uni.showToast({
                title: "下单成功",
                icon: "success",
                position: "top",
                duration: 1000,
              });
              setTimeout(() => {
                uni.navigateTo({
                  url: "/pages/mail/sent-off-check",
                });
              }, 2000);
            })
            .catch((err) => {
              uni.showToast({
                title: "下单失败",
                icon: "error",
                position: "top",
                duration: 1000,
              });
              console.log(err);
            });
        }
      }
      if (reason1 === 1) {
        if (mode1 === 0) {
          //上门取件
          mailAPI
            .insertMailReason1WithVisitTime(params3)
            .then((res) => {
              console.log(res);
              uni.showToast({
                title: "下单成功",
                icon: "success",
                position: "top",
                duration: 1000,
              });
              setTimeout(() => {
                uni.navigateTo({
                  url: "/pages/mail/sent-off-check",
                });
              }, 2000);
            })
            .catch((err) => {
              uni.showToast({
                title: "下单失败",
                icon: "error",
                position: "top",
                duration: 1000,
              });
              console.log(err);
            });
        } else {
          mailAPI
            .insertMailReason1WithStageName(params4)
            .then((res) => {
              console.log(res);
              uni.showToast({
                title: "下单成功",
                icon: "success",
                position: "top",
                duration: 1000,
              });
              setTimeout(() => {
                uni.navigateTo({
                  url: "/pages/mail/sent-off-check",
                });
              }, 2000);
            })
            .catch((err) => {
              uni.showToast({
                title: "下单失败",
                icon: "error",
                position: "top",
                duration: 1000,
              });
              console.log(err);
            });
        }
      }
    },
  },
};
</script>
<style lang='scss' scoped>
</style>